package com.zzh.oauth.intergration.authenticator;

import com.zzh.oauth.intergration.IntegrationAuthenticationEntity;
import com.zzh.oauth.intergration.pojo.UserPojo;
import com.zzh.oauth.intergration.service.U2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * u2登录认证器
 */
@Component
public class U2Authenticator extends AbstractPreparableIntegrationAuthenticator {
    private final static String AUTH_TYPE = "u2";
//    @Autowired
//    private UserMapper mapper;

    @Autowired
    private U2Service u2Service;

    @Override
    public UserPojo authenticate(IntegrationAuthenticationEntity entity) {
        String u2code = entity.getAuthParameter("u2code");
        String u2state = entity.getAuthParameter("u2state");
        if (StringUtils.isEmpty(u2code) || StringUtils.isEmpty(u2state)){
            throw new OAuth2Exception("登录失败");
        }

        String accessToken = u2Service.getAccessToken(u2code, u2state);
        UserPojo userPojo = new UserPojo();
        userPojo.setMobile("12345");
        userPojo.setName("12345");
        userPojo.setPwd("12345");
        return userPojo;
    }

    @Override
    public boolean support(IntegrationAuthenticationEntity entity) {
        return AUTH_TYPE.equals(entity.getAuthType());
    }

}
